﻿using IOP.SgrA;
using IOP.SgrA.ECS;
using IOP.SgrA.Input;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Text;

namespace VkSample97
{
    public class TransformSystem : ComponentSystem
    {
        private Camera Camera;
        private IGraphicsManager GraphicsManager;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="contextManager"></param>
        public TransformSystem(IGraphicsManager manager)
        {
            GraphicsManager = manager;
        }

        public override void Initialization()
        {
            GraphicsManager.TryGetCamera("main", out Camera);
        }

        public override void Update(TimeSpan timeSpan)
        {
            if (Input.GetMouseButtonState(MouseButton.ButtonLeft).Action == InputAction.Press)
            {
                var delta = Input.GetMousePositionDelta();
                if (delta.X != 0 || delta.Y != 0)
                {
                    Camera.RotationFromMouse(delta.X, delta.Y, (float)(0.2 * Math.PI / 180.0));
                }
            }
            ContextManager.Foreach<SkyboxGroup>((group) =>
            {
                Context context = ContextManager.GetContext(group.Entity);
                ref MVPMatrix mvp = ref context.GetMVPMatrix();
                var renderObject = context.GetContextRenderObject();
                Transform transform = renderObject.GetComponents<Transform>().First();
                mvp.ModelMatrix = Matrix4x4.CreateFromQuaternion(transform.Rotation) * 
                Matrix4x4.CreateTranslation(transform.Position);
                context.PushToRenderGroup();
            });
        }
    }
}
